import os


# XOR加密函数
def xor_encrypt(data, key):
    return bytes([a ^ key for a in data])


# XOR解密函数
def xor_decrypt(data, key):
    return bytes([a ^ key for a in data])


# 加密文件
def encrypt_file(file_path, key):
    with open(file_path, 'rb') as file:
        data = file.read()
    encrypted_data = xor_encrypt(data, key)
    with open(file_path, 'wb') as file:
        file.write(encrypted_data)


# 解密文件
def decrypt_file(file_path, key):
    with open(file_path, 'rb') as file:
        data = file.read()
    decrypted_data = xor_decrypt(data, key)
    with open(file_path, 'wb') as file:
        file.write(decrypted_data)


# 使用示例
file_path = 'example.txt'  # 待加密的文件路径
key = 0xFF

# 加密文件
encrypt_file(file_path, key)

# 解密文件
decrypt_file(file_path, key)

# 确保文件内容被修改
assert open(file_path, 'rb').read() == open(file_path, 'rb').read()